﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BO.Management.PowerShell.InternalHelper
{
    public sealed class ProviderNavigationHelper
    {
        /// <summary>
        /// The separator used in path statements.
        /// </summary>
        public const string PathSeparator = "\\";

        /// <summary>
        /// Modifies the path ensuring that the correct path separator
        /// character is used.
        /// </summary>
        /// <param name="path">Path to normanlize.</param>
        /// <returns>Normanlized path.</returns>
        public static string NormalizePath(string path)
        {
            string result = path;

            if (!String.IsNullOrEmpty(path))
            {
                result = path.Replace("/", ProviderNavigationHelper.PathSeparator);
            }

            return result;
        } // NormalizePath

        /// <summary>
        /// Breaks up the path into individual elements.
        /// </summary>
        /// <param name="path">The path to split.</param>
        /// <returns>An array of path segments.</returns>
        public static string[] ChunkPath(string root, string path)
        {
            // Normalize the path before splitting
            string normalPath = NormalizePath(path);

            // Return the path with the drive name and first path 
            // separator character removed, split by the path separator.
            string pathNoDrive = normalPath.Replace(
                                       root + ProviderNavigationHelper.PathSeparator,
                                       string.Empty);
            return pathNoDrive.Split(ProviderNavigationHelper.PathSeparator.ToCharArray());
        } // End ChunkPath method.


        /// <summary>
        /// Checks to see if a given path is actually a drive name.
        /// </summary>
        /// <param name="path">The path to check.</param>
        /// <returns>
        /// True if the path given represents a drive, otherwise false 
        /// is returned.
        /// </returns>
        public static bool PathIsDrive(string root, string path)
        {
            // Remove the drive name and first path separator.  If the 
            // path is reduced to nothing, it is a drive. Also, if it is
            // just a drive then there will not be any path separators.
            if (String.IsNullOrEmpty(
                         path.Replace(root, string.Empty)) ||
                         String.IsNullOrEmpty(
                         path.Replace(root + ProviderNavigationHelper.PathSeparator, string.Empty)))
            {
                return true;
            }
            else
            {
                return false;
            }
        } // End PathIsDrive method.



    }
}
